﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using NHibernate;
using NHibernate.Criterion;

using ProjectBase.Core;
using ProjectBase.Data.NH;

namespace ProjectBase.Data
{
    public static partial class DaoExtensions
    {
        #region Criteria Equal

        /// <summary>
        ///     Appends filter name = value. If value is null, no criteria is created
        /// </summary>
        /// <param name = "criteria"></param>
        /// <param name = "name"></param>
        /// <param name = "value"></param>
        public static ICriteria AddEqual<TValue>(this ICriteria criteria, string name, TValue? value)
            where TValue : struct
        {
            if (value.Is())
            {
                criteria.Add(new SimpleExpression(name, value, Const.Equal, false));
            }
            return criteria;
        }

        /// <summary>
        ///     Creates equals statement for object comparing
        /// </summary>
        /// <param name = "criteria"></param>
        /// <param name = "name"></param>
        /// <param name = "value"></param>
        public static ICriteria AddEqual(this ICriteria criteria, string name, object value)
        {
            if (value.Is())
            {
                criteria.Add(new SimpleExpression(name, value, Const.Equal, false));
            }
            return criteria;
        }
        #endregion Criteria Equal

        #region Criteria NOT Equal
        /// <summary>
        ///     Creates criteria with &lt;&gt; statement
        /// </summary>
        /// <param name = "criteria"></param>
        /// <param name = "name"></param>
        /// <param name = "value"></param>
        /// <returns></returns>
        public static ICriteria AddNotEqual(this ICriteria criteria, string name, int? value)
        {
            if (value.Is())
            {
                criteria.Add(new SimpleExpression(name, value, Const.NotEqual, false));
            }
            return criteria;
        }

        #endregion Criteria NOT Equal

        #region Criteria Like for string
        /// <summary>
        ///     Creates Like for strings with * or % and == for other
        /// </summary>
        /// <param name = "criteria"></param>
        /// <param name = "name"></param>
        /// <param name = "value"></param>
        public static ICriteria AddLike(this ICriteria criteria, string name, string value)
        {
            if (value.IsEmpty() || value == "*" || value == "%" || name.IsEmpty())
            {
                return criteria;
            }
            criteria.Add(new LikeCollationExpression(name, value.Replace(Const.Asterisk, Const.Percentage)));
            //if (value.IndexOf(Const.Asterisk, StringComparison.OrdinalIgnoreCase) < 0)
            //{
            //    // true should be provided by CaseInsensitive setting on SQL Server, if not - then we should not change it....
            //    criteria.Add(new SimpleExpression(name, value, Const.Equal, false));
            //    //criteria.Add(Expression.Sql(
            //    //    "{0} = ? COLLATE ".FormatWith(name, Global.Settings.DaoDefaultLikeCollation)
            //    //    , value.Replace(Const.Asterisk, Const.Percentage)
            //    //    , NHibernateUtil.String));
            //}
            //else
            //{
            //    //criteria.Add(new LikeExpression(name, value.Replace(Const.Asterisk, Const.Percentage)));
            //    criteria.Add(new LikeCollationExpression(name, value.Replace(Const.Asterisk, Const.Percentage)));
            //    //criteria.Add(Expression.Sql(
            //    //    " {0} LIKE ? COLLATE {1} ".FormatWith(column, Global.Settings.DaoDefaultLikeCollation)
            //    //    , value.Replace(Const.Asterisk, Const.Percentage)
            //    //    , NHibernateUtil.String));
            //}
            return criteria;
        }

        public static ICriteria AddNotLike(this ICriteria criteria, string name, string value)
        {
            if (value.IsEmpty() || value == "*" || value == "%")
            {
                return criteria;
            }
            if (value.IndexOf(Const.Asterisk, StringComparison.OrdinalIgnoreCase) < 0)
            {
                // true should be provided by CaseInsensitive setting on SQL Server, if not - then we should not change it....
                criteria.Add(new SimpleExpression(name, value, Const.NotEqual, false));
            }
            else
            {
                criteria.Add(
                    new NotExpression(new LikeExpression(name,
                        value.Replace(Const.Asterisk,
                            Const.Percentage))));
            }
            return criteria;
        }
        #endregion Criteria Like for string

        #region Criteria IsNull
        /// <summary>
        ///     Creates IsNull for True;
        ///     IsNotNull for false;
        ///     nothing for null
        /// </summary>
        /// <param name = "criteria"></param>
        /// <param name = "name"></param>
        /// <param name = "value"></param>
        public static ICriteria AddIsNull(this ICriteria criteria, string name, bool? value)
        {
            if (value.Is())
            {
                if (value.Value)
                {
                    criteria.Add(new NullExpression(name));
                }
                else
                {
                    criteria.Add(new NotNullExpression(name));
                }
            }
            return criteria;
        }
        #endregion Criteria IsNull

        #region Criteria Is IN
        /// <summary>
        ///     Creates the IN clause for the list of sent ID's
        /// </summary>
        /// <param name = "criteria">Existing criteria</param>
        /// <param name = "name">the name of the property which should meet the IN clause</param>
        /// <param name = "values">The list of ID's which are in the IN clause</param>
        public static ICriteria CreateIdisIn(this ICriteria criteria, string name, IEnumerable<int> values)
        {
            if (values.Is()
                &&
                values.Count() > 0)
            {
                criteria.Add(new InExpression(name, values.Select<int, object>(i => i).ToArray()));
            }
            return criteria;
        }
        #endregion Criteria Is IN

    }
}